package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.hfb.CoreUtils;
import com.atguigu.srb.core.pojo.entity.Lend;
import com.atguigu.srb.core.pojo.entity.LendItem;
import com.atguigu.srb.core.pojo.entity.LendItemReturn;
import com.atguigu.srb.core.mapper.LendItemReturnMapper;
import com.atguigu.srb.core.pojo.entity.LendReturn;
import com.atguigu.srb.core.service.LendItemReturnService;
import com.atguigu.srb.core.service.LendItemService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 标的出借回款记录表 服务实现类
 * </p>
 *
 * @author Atguigu
 * @since 2022-11-19
 */
@Service
public class LendItemReturnServiceImpl extends ServiceImpl<LendItemReturnMapper, LendItemReturn> implements LendItemReturnService {
    @Resource
    LendItemService lendItemService;
    public List<LendItemReturn> getLendItemReturns(Lend lend, List<LendReturn> lendReturns) {
        Map<Integer, Long> map = lendReturns.stream()
                .collect(Collectors.toMap(LendReturn::getCurrentPeriod, LendReturn::getId));
        //一个借款的投资项有多个，
        //获取标的 投资成功的投资项集合: 所有的投资项 第N期  回款的本金之和 等于 还款计划这一期的本金  ，利息也一样
        List<LendItem> lendItemList = lendItemService.list(Wrappers.lambdaQuery(LendItem.class)
                .eq(LendItem::getLendId, lend.getId())
                .eq(LendItem::getStatus, 1));
        List<LendItemReturn>  allLendItemReturns = new ArrayList<>();//保存所有投资人 的回款计划
        lendItemList.forEach(lendItem -> {
            Map<Integer, BigDecimal> perMonthPrincipal2 = null;
            Map<Integer, BigDecimal> perMonthInterest2 = null;
            CoreUtils.initPerMonthMap(perMonthPrincipal2,perMonthInterest2 ,lendItem.getInvestAmount(), lend.getLendYearRate(), lend.getPeriod(),lend.getReturnMethod() );
            List<LendItemReturn> lendItemReturns = new ArrayList<>();
            //还款方式为4：只生成最后一期的回款计划
            if(lend.getReturnMethod()==4){
                //lend_item_return:
                LendItemReturn lendItemReturn = new LendItemReturn();
                //对应的还款计划id
                lendItemReturn.setLendReturnId(map.get(lend.getPeriod()));
                //投资项id
                lendItemReturn.setLendItemId(lendItem.getId());
                lendItemReturn.setLendId(lendItem.getLendId());
                lendItemReturn.setInvestUserId(lendItem.getInvestUserId());
                lendItemReturn.setInvestAmount(lendItem.getInvestAmount());
                lendItemReturn.setCurrentPeriod(lend.getPeriod());
                lendItemReturn.setLendYearRate(lendItem.getLendYearRate());
                lendItemReturn.setReturnMethod(lend.getReturnMethod());
                lendItemReturn.setPrincipal(perMonthPrincipal2.get(1));
                lendItemReturn.setInterest(perMonthInterest2.get(1));
                lendItemReturn.setTotal(lendItemReturn.getPrincipal().add(lendItemReturn.getInterest()));
                lendItemReturn.setFee(new BigDecimal("0"));
                lendItemReturn.setReturnDate(new DateTime(lendItem.getLendStartDate()).plusMonths(lend.getPeriod()).toDate());
                lendItemReturn.setRealReturnTime(null);
                lendItemReturn.setOverdue(null);
                lendItemReturn.setOverdueTotal(null);
                lendItemReturn.setStatus(0);
                lendItemReturns.add(lendItemReturn);
            }else{
                for (Integer i = 1; i <= lend.getPeriod(); i++) {
                    //lend_item_return:
                    LendItemReturn lendItemReturn = new LendItemReturn();
                    //对应的还款计划id
                    lendItemReturn.setLendReturnId(map.get(i));
                    //投资项id
                    lendItemReturn.setLendItemId(lendItem.getId());
                    lendItemReturn.setLendId(lendItem.getLendId());
                    lendItemReturn.setInvestUserId(lendItem.getInvestUserId());
                    lendItemReturn.setInvestAmount(lendItem.getInvestAmount());
                    lendItemReturn.setCurrentPeriod(i);
                    lendItemReturn.setLendYearRate(lendItem.getLendYearRate());
                    lendItemReturn.setReturnMethod(lend.getReturnMethod());
                    lendItemReturn.setPrincipal(perMonthPrincipal2.get(i));
                    lendItemReturn.setInterest(perMonthInterest2.get(i));
                    lendItemReturn.setTotal(lendItemReturn.getPrincipal().add(lendItemReturn.getInterest()));
                    lendItemReturn.setFee(new BigDecimal("0"));
                    lendItemReturn.setReturnDate(new DateTime(lendItem.getLendStartDate()).plusMonths(i).toDate());
                    lendItemReturn.setRealReturnTime(null);
                    lendItemReturn.setOverdue(null);
                    lendItemReturn.setOverdueTotal(null);
                    lendItemReturn.setStatus(0);
                    lendItemReturns.add(lendItemReturn);
                }
            }
            //每个投资项都应该生成 还款期数个回款计划
            this.saveBatch(lendItemReturns);
            allLendItemReturns.addAll(lendItemReturns);
        });
        return allLendItemReturns;
    }

}
